<?php

define(DB_HOST, 'localhost');  
define(DB_USER, 'root');  
define(DB_PASS, '54133');  
define(DB_DATABASENAME, 'wxmp_zxk');  

define(SMTP_PORT,25);

/*
define(SMTP_SERVER,"smtp.qq.com");
define(SMTP_USER,"380447834");
define(SMTP_EMAIL,SMTP_USER."@qq.com");
define(SMTP_PASS,"");
*/

define(SMTP_SERVER,"smtp.qq.com");
define(SMTP_USER,"249750689");
define(SMTP_EMAIL,SMTP_USER."@qq.com");
define(SMTP_PASS,"");



define(PROJECT_NAME,"wxmp-zxk");

define(PROJECT_CNNAME,"宅修客");
define(PROJECT_ADDR,"广州市白云区新市墟太阳广场一层C19");
define(PROJECT_TEL,"400-624-2422");




$DICT_COLOR_NAME=array("B"=>"黑色","W"=>"白色","G"=>"金色","P"=>"玫瑰金");
$DICT_COLOR_RGB=array("B"=>"#444","W"=>"#fff","G"=>"#e3d0ba","P"=>"#FFC1C1");

$DICT_ORDERTYPE_NAME=array("phonefix"=>"手機維修","padfix"=>"平板維修","recovery"=>"手機回收","memoryup"=> "容量升級");

$DICT_SERVICE_NAME=array("express"=>"到店/邮寄维修","visit"=>"上门维修");

require_once "lib/smtp.php";


function getConnection(){

	  $conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("connect failed" . mysql_error());  
       mysql_select_db(DB_DATABASENAME, $conn);  
        mysql_query("set character set 'utf8'");
	   return $conn;

}


function fetch_rows($sql){

	$conn=getConnection();
	$result=mysql_query($sql, $conn);
	 mysql_data_seek($result, 0);
	$rows=array();
    while ($row=mysql_fetch_assoc($result))
    {
         array_push($rows,$row); 
	}
	mysql_free_result($result);  
    mysql_close($conn);  
	return $rows;
}



function insert_row($sql){

	$conn=getConnection();
	mysql_query("set names utf8;");
	$result = mysql_query("insert ".$sql,$conn);
    return  mysql_insert_id();

}


function update_row($sql){

	$conn=getConnection();
	mysql_query("set names utf8;");
	$result = mysql_query("update ".$sql,$conn);
    return  $result;

}

function getColorName($code){

    global $DICT_COLOR_NAME;
	return $DICT_COLOR_NAME[$code];

}


function getColorRGB($code){

	global $DICT_COLOR_RGB;

	return $DICT_COLOR_RGB[$code];

}

function getServiceName($code){

	global $DICT_SERVICE_NAME;
	return $DICT_SERVICE_NAME[$code];

}

function getOrderTypeName($code){

	global $DICT_ORDERTYPE_NAME;

	return $DICT_ORDERTYPE_NAME[$code];

}

function currCustomer(){
    session_start();
	$customer_id=$_SESSION["customer_id"];
	$sql="select * from customer_detail where id=$customer_id ";
	$users=fetch_rows($sql);
    return $users[0];
}



function sendEmail($toemail,$mailtitle,$mailcontent){


	$mailtype = "HTML";//邮件格式（HTML/TXT）,TXT为文本邮件
	//************************ 配置信息 ****************************
	$smtp = new smtp(SMTP_SERVER,SMTP_PORT,true,SMTP_USER,SMTP_PASS);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
	$smtp->debug = false;//是否显示发送的调试信息
	$state = $smtp->sendmail($toemail, SMTP_EMAIL, $mailtitle, $mailcontent, $mailtype);
	return $state;

}

function sendEmailToAdmin($mailtitle,$mailcontent){

	$admin_emails="";
    
	$sql="select * from admin where state='enable' ";

	$rows=fetch_rows($sql);

	foreach($rows as $row){

        $admin_emails.=$row["email"].",";        

	}

	return sendEmail($admin_emails,$mailtitle,$mailcontent);

}


function htmlForOrderSum($order_sum){
   
    $seviceName=getServiceName($order_sum["service_method"]);
    $device_colorName=getColorName($order_sum["device_color"]);

	$content="<p>订单号:".$order_sum["code"]."</p>"
               ."<p>下单时间：".$order_sum["created_at"]."</p>"
               ."<p>设备机型：".$order_sum["device_name"]." $device_colorName</p>"
               ."<p>服務明細:".$order_sum["remark"]."</p>"
               ."<p>预估金额：".$order_sum["total_price"]."</p>"
               ."<p>服务类型：$seviceName</p>";
               switch($order_sum["service_method"]){

				   case "express":
				   $content.="<p>快递单号:".$order_sum["express_code"]."</p>"
					   ."<p>发件人:".$order_sum["express_sender_name"]."</p>"
					   ."<p>寄回地址:".$order_sum["express_sender_address"]."</p>"
					   ."<p>联系电话:".$order_sum["express_sender_tel"]."</p>"

					   ;
				   break;
                   case "visit":
				   $content.="<p>联系人:".$order_sum["visit_contact_realname"]."</p>"
					   ."<p>手机号:".$order_sum["visit_contact_tel"]."</p>"
					   ."<p>详细地址:".$order_sum["visit_contact_address"]."</p>"
                        ."<p>上门时间:".$order_sum["visit_arrive_at"]."</p>"
					   ;

				   break;
			   }
     return $content;

}


function getBasePath(){
   $port=$_SERVER["SERVER_PORT"];
   $host=$_SERVER["HTTP_HOST"];
   if ($port==80){
      $bp="http://$host/".PROJECT_NAME;
   }else{
       $bp="http://$host:$port/".PROJECT_NAME;
   }
   return $bp;
}


function location_to($url){

	//echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"1;url=$url\">";
	header("location: $url");
    exit;

}

function echo_json($obj){
    
    header('Content-type: application/json; charset=utf-8');
	echo json_encode($obj);
	exit();
    
}



?>
